package com.tong.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tong.wms.config.QueryPageParam;
import com.tong.wms.config.Result;
import com.tong.wms.pojo.Goodstype;
import com.tong.wms.service.GoodstypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/goodstype")
public class GoodstypeController {

    @Resource
    private GoodstypeService goodstypeService;

    @GetMapping("/select")
    public List<Goodstype> select(@RequestBody Goodstype goodstype) {
        LambdaQueryWrapper<Goodstype> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Goodstype::getName, goodstype.getName());
        return goodstypeService.list(queryWrapper);
    }

    @PostMapping("/list")
    public Result list() {
        List<Goodstype> list = goodstypeService.list();
        return Result.success(list);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Goodstype goodstype) {
        return goodstypeService.save(goodstype) ? Result.success() : Result.fail();
    }

    @PostMapping("/update")
    public Result mod(@RequestBody Goodstype goodstype) {
        return goodstypeService.updateById(goodstype) ? Result.success() : Result.fail();
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Goodstype goodstype) {
        return goodstypeService.saveOrUpdate(goodstype) ? Result.success() : Result.fail();
    }

    @DeleteMapping("/del")
    public Result del(@RequestParam int id) {
        return goodstypeService.removeById(id) ? Result.success() : Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String) param.get("name");

        Page<Goodstype> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Goodstype> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Goodstype::getName, name);
        }

        IPage<Goodstype> result = goodstypeService.page(page, lambdaQueryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }
}
